define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'type',title: __('类型'),formatter: Table.api.formatter.status,searchList: {'10': __('普通用户'), '20': __('虚拟用户')}},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'address',title: __('所在城市'), operate: false},
                        // {field: 'email', title: __('Email'), operate: 'LIKE'},
                        // {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        // {field: 'level', title: __('Level'), operate: 'BETWEEN', sortable: true},
                        {field: 'gender', title: __('性别'),formatter: Table.api.formatter.status, searchList: {0:__('未知'),1: __('Male'), 2: __('Female')}},
                        {field: 'age', title: __('年龄'), operate: 'BETWEEN'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'constellation', title: __('星座'),formatter: Table.api.formatter.status, searchList: {1:__('白羊座'),2: __('金牛座'), 3: __('双子座'), 4: __('巨蟹座'), 5: __('狮子座'), 6: __('处女座'), 7: __('天秤座'), 8: __('天蝎座'), 9: __('射手座'),10: __('摩羯座'), 11: __('水瓶座'), 12: __('双鱼座')}},
                        {field: 'weight', title: __('体重'), operate: 'BETWEEN'},
                        {field: 'height', title: __('身高'), operate: 'BETWEEN'},
                        {field: 'hobby', title: __('爱好')},
                        {field: 'device_num', title: __('设备数量'), sortable: true},
                        // {field: 'score', title: __('Score'), operate: 'BETWEEN', sortable: true},
                        // {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true}, // visible: false 列表不显示此字段
                        // {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        // {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        {field: 'createtime', title: __('注册时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        // {field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        {field: 'is_custom_service', title: __('是否为客服'), formatter: Table.api.formatter.status, searchList: {'1': __('Yes'), '0': __('No')}},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {'normal': __('正常'), 'cancellation': __('注销'), 'frozen': __('冻结')}},
                        {
                            field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'frozen',
                                    text: __('冻结'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: 'user/user/frozen',
                                    confirm: '确认是否要冻结',
                                    success: function (data, ret) {
                                        if(ret["code"]){
                                            $(".btn-refresh").trigger("click");
                                            Layer.alert("已冻结");
                                        }
                                        else {
                                            Layer.alert('<span id="" style="color: red;">'+ret["msg"]+'</span>');
                                        }

                                    },
                                    //visible属性可以让按钮显示或者隐藏，这里通过判断status = 6就显示，否则就隐藏
                                    visible:function(row){
                                        console.log(row)
                                        if(row.status == 20 || row.status == 30){
                                            return false
                                        }else{
                                            return true
                                        }
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                },
                                {
                                    name: 'thaw',
                                    text: __('解冻'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    icon: 'fa fa-magic',
                                    url: 'user/user/thaw',
                                    confirm: '确认是否要解冻',
                                    success: function (data, ret) {
                                        if(ret["code"]){
                                            $(".btn-refresh").trigger("click");
                                            Layer.alert("已解冻");
                                        }
                                        else {
                                            Layer.alert('<span id="" style="color: red;">'+ret["msg"]+'</span>');
                                        }

                                    },
                                    //visible属性可以让按钮显示或者隐藏，这里通过判断status = 6就显示，否则就隐藏
                                    visible:function(row){ // false: 不显示按钮， true: 显示按钮
                                        console.log(row)
                                        if(row.status == 30){
                                            return true
                                        }else{
                                            return false
                                        }
                                    },
                                    error: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});